/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : home.js                                 
*  Description  : 클럽개별메인 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-20  이윤노    Created.  
*  2012-06-00  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){
	function clsNoticeDetail(){
		this._HTMLDom = null;
		this.listData = null;
		this.listPageData = null;
		this._init();	
	}
		
	clsNoticeDetail.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content");
			this._HTMLDom = {
				"$layer": $layer	
			}
			$layer.find("table").hide();
			$layer.find("ul").hide();
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setData: function(thisData){
			this.listData = thisData;
			this.display();	
		}
		, setPageData: function(thisData){
			this.listPageData = thisData;
			this.displayPage();	
		}
		, displayPage: function(){
			var thisClass = this;
			var $layer = this.$getLayer();
			
			$layer.find("ul.list_pass a.prev").html("<span>이전글</span>"+thisClass.listPageData["이전글"]["제목"]);			
			$layer.find("ul.list_pass a.next").html("<span>다음글</span>"+thisClass.listPageData["다음글"]["제목"]);
			
			$layer.find("ul").show();
		}
		, display: function(){
			var thisClass = this;
			var $layer = this._HTMLDom["$layer"];
				
			$layer.find("table thead tr th").html(thisClass.listData["제목"]);
			$layer.find("dl.fl dd:eq(0)").html(thisClass.listData["원본파일명"]);
			$layer.find("table tbody tr:eq(0) td:eq(1) dd:eq(0)").html(thisClass.listData["작성일자"] + " "  + thisClass.listData["작성시간"]);
			$layer.find("table tbody tr:eq(0) td:eq(1) dd:eq(1)").html(_util.number_format(thisClass.listData["조회수"]));
			$layer.find("table tbody tr:eq(1) td div").html(thisClass.listData["내용"]);
		
			$layer.find("table").show();
		}
		, $getBtnPrev: function(){
			return this._HTMLDom["$layer"].find("ul li a.prev");	
		}
		, $getBtnNext: function(){
			return this._HTMLDom["$layer"].find("ul li a.next");
		}
	}

	
	function clsComm()
	{
		this.noticeDetail = null;
		this.noticeDetailRequest = null;
		
		this.noticePage = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){		
			this.noticeDetailRequest = {
				"게시판키": null	
			}
		}
//------------------------ 공지사항상세------------------------//	
		, getNoticeDetail: function(){
			var thisClass = this;
			
			var szServicename = "getDetailNotice";
			var szNoticeKey = this.noticeDetailRequest["게시판키"];
						
			// ajax 시작
			_gf.ajax({
				"sid":"MB001",
				"servicename": szServicename,
			}, {
				"board_seq": szNoticeKey
				, "modpage_yn": "N"
			}, function(ret){
				// callback and mapping
				if(ret["msp"]["contents"]["items"]){					
					
					var obj = ret["msp"]["contents"]["items"];
					var d = _util.nvl(obj["reg__date"])+"";
					var t = _util.nvl(obj["reg__time"])+"";
											
					thisClass.noticeDetail = {
						"게시판키": _util.nvl(obj["board__seq"])
						, "제목": _util.nvl(obj["title"])
						, "작성일자": d.substrEx(0, 4) + "-" + d.substrEx(4, 2) + "-" + d.substrEx(6, 2)
						, "작성시간": t.substrEx(0, 2) + "-" + t.substrEx(2, 2)
						, "조회수": _util.nvl(obj["hit"], 0)
						, "내용": _util.nvl(obj["content"])
						, "파일명": ""
						, "원본파일명": ""
					};
				}				
				$(thisClass).trigger("evtFinishNoticeDetail" );
			});
			// ajax 끝
		}
		, getNoticeDetailData: function(){
			return this.noticeDetail;
		}
		, setNoticeDetailRequest: function(key, val){
			this.noticeDetailRequest[key] = val;	
		}
//------------------------ 공지사항상세------------------------//			
//------------------------ 이전/다음글------------------------//	
		, getNoticePage: function(){
			var thisClass = this;
			
			var szServicename = "getNoticeReviewList";
			var szNoticeKey = this.noticeDetailRequest["게시판키"];
			
			// ajax 시작
			_gf.ajax({
				"sid":"MB001",
				"servicename": szServicename,
			}, {
				"board_seq": szNoticeKey
			}, function(ret){
				
				var pre = {
					"게시판키":null
					, "제목": "이전글이 없습니다."
				};
				var nex = {
					"게시판키":null
					, "제목": "다음글이 없습니다."
				};
				
				// callback and mapping
				if(ret["msp"]["contents"]["items"]){					
					
					if(!ret["msp"]["contents"]["items"].length){
						ret["msp"]["contents"]["items"]	= new Array(ret["msp"]["contents"]["items"]);
					}
					
					for(var i=0;i<ret["msp"]["contents"]["items"].length;++i){
						var obj = ret["msp"]["contents"]["items"][i];
						
						if(obj["list__type"] == "pre"){						
							pre = {
								"게시판키": _util.nvl(obj["board__seq"])
								, "제목": _util.nvl(obj["title"])
							}	
						}else if(obj["list__type"] == "next"){						
							nex = {
								"게시판키": _util.nvl(obj["board__seq"])
								, "제목": _util.nvl(obj["title"])
							}	
						}
						
					}
						
					thisClass.noticePage = {						
								"이전글" : pre
								, "다음글": nex
					};
				}				
				$(thisClass).trigger("evtFinishNoticePage" );
			});
			// ajax 끝
		}
		, getNoticePageData: function(){
			return this.noticePage;
		}		
//------------------------ 이전/다음글------------------------//
	}
	
	function clsControl()
	{
		this.notice_key = null;
		this.page = null;
		
		this.objComm = null;
		
		this.objNoticeDetail = null;
	
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objNoticeDetail = new clsNoticeDetail();
			
			this.notice_seq = _util.getParam("notice_seq");
			this.page = _util.getParam("page");
			
			this._bind();
			
			//공지사항조회 
			_gf.spinStart();
			this.objComm.setNoticeDetailRequest("게시판키",this.notice_seq);
			this.objComm.getNoticeDetail();
			this.objComm.getNoticePage();
		}
		, _bind: function(){
			var thisClass = this;
			
			//공지사항조회 완료 이벤트
			$(this.objComm).bind("evtFinishNoticeDetail", function(){ 
				var thisData = thisClass.objComm.getNoticeDetailData();
				thisClass.objNoticeDetail.setData(thisData);
				thisClass.notice_seq = thisData["게시판키"];
				_gf.spinEnd();
			});
			
			//공지사항조회 완료 이벤트
			$(this.objComm).bind("evtFinishNoticePage", function(){ 
				var thisData = thisClass.objComm.getNoticePageData();
				thisClass.objNoticeDetail.setPageData(thisData);				
			});
			
			//이전글 클릭이벤트
			this.objNoticeDetail.$getBtnPrev().click(function(){
				if(!thisClass.objNoticeDetail.listPageData["이전글"]["게시판키"]){
					//alert("이전글이 없습니다.");	
					return;
				}
				_gf.spinStart();
				thisClass.objComm.setNoticeDetailRequest("게시판키",thisClass.objNoticeDetail.listPageData["이전글"]["게시판키"]);
				thisClass.objComm.getNoticeDetail();
				thisClass.objComm.getNoticePage();
			});
			
			//다음글 클릭이벤트
			this.objNoticeDetail.$getBtnNext().click(function(){
				if(!thisClass.objNoticeDetail.listPageData["다음글"]["게시판키"]){
					//alert("다음글이 없습니다.");	
					return;
				}
				_gf.spinStart();
				thisClass.objComm.setNoticeDetailRequest("게시판키",thisClass.objNoticeDetail.listPageData["다음글"]["게시판키"]);
				thisClass.objComm.getNoticeDetail();
				thisClass.objComm.getNoticePage();
			});
			
			//목록
			$("article.btn_area a img[alt=목록]").click(function(event){
				document.location.href="notice.html" + thisClass.getParam();
			});
		}
		, getParam: function(){
			return "?page=" + this.page;	
		}
		, getFormatDateYmd: function(n,v){			
			if(n=="") return n;
			n = String(n);
			
			return n.substr(0,4) +v+ n.substr(4,2) +v+ n.substr(6,2);
		}
		, getFormatTime: function(n,v){		
			if(n=="") return n;
			n = String(n);			
			return n.substr(0,2) +v+ n.substr(2,2);
		}
		, getFormatNumber: function(n){
			if(n=="") return n;
			n = String(n);			
			i = n.length;
			r = "";
			while(true){				
				if(i<=3) {
					r = n.substr(0,i) + r;					
					break;
				}
				r = ","+ n.substr(i-3,3) + r;				
				i-=3;
			}
			return r;
		}	
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});	